//
//  SEGWinRecordListViewCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/21.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGWinRecordListViewCell.h"
@interface SEGWinRecordListViewCell ()
/**
 奖品名称
 */
@property (nonatomic, strong)UILabel *prizeNameLabel;

/**
 活动名称
 */
@property (nonatomic, strong)UILabel *actNameLabel;

/**
 奖品发放状态(1:已发放,2:未发放)
 */
@property (nonatomic, strong)UILabel *statusLabel;

/**
 中奖时间
 */
@property (nonatomic, strong)UILabel *timeLabel;

@end

@implementation SEGWinRecordListViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    _prizeNameLabel = [[UILabel alloc] init];
    _prizeNameLabel.font = kFont_16;
    _prizeNameLabel.textColor = kColor_1;
    [self.contentView addSubview:_prizeNameLabel];
    
    _actNameLabel = [[UILabel alloc] init];
    _actNameLabel.font = kFont_13;
    _actNameLabel.textColor = kColor_3;
    [self.contentView addSubview:_actNameLabel];
    
    _statusLabel = [[UILabel alloc] init];
    _statusLabel.font = kFont_13;
    [self.contentView addSubview:_statusLabel];
    [_statusLabel sizeToFit];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = kFont_13;
    _timeLabel.textColor = kColor_3;
    [self.contentView addSubview:_timeLabel];
    [_timeLabel sizeToFit];
    
    [_prizeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView).offset(14);
        make.height.mas_greaterThanOrEqualTo(16);
    }];
    
    [_actNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.bottom.equalTo(self.contentView).offset(-14);
        make.height.mas_greaterThanOrEqualTo(13);
    }];
    
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15).priorityHigh();
        make.bottom.equalTo(_prizeNameLabel);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15).priorityHigh();
        make.top.equalTo(_actNameLabel);
    }];
}

- (void)setUpCellWithModel:(SEGWinRecordModel *)model
{
    self.prizeNameLabel.text = model.prizeName;
    self.actNameLabel.text = model.actName;
    
    if (model.isInvalid == 1) {
        self.statusLabel.text = @"已失效";
        self.statusLabel.textColor = kColor_3;
    } else {
        if ([model.sendStatus isEqualToString:@"1"]) {//奖品发放状态(1:已发放,2:未发放)
            self.statusLabel.text = @"已发放";
            self.statusLabel.textColor = kColor_T;
        }else {
            self.statusLabel.text = @"未发放";
            self.statusLabel.textColor = kColor_1;
        }
    }
    
    CGFloat statusWidth = [self.statusLabel.text sizeWithFont:_statusLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping].width;
    
    [_prizeNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(LYScreenWidth() - 42 - statusWidth);
    }];
    [_statusLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_greaterThanOrEqualTo(statusWidth);
    }];
    
    NSString *time = [SEGUtils stringDateByFormatString:@"yyyy年MM月dd日 HH:mm:ss" withDate:[NSDate dateWithTimeIntervalSince1970:[model.winnerTime doubleValue]]];
    self.timeLabel.text = time;
    CGFloat timeWidth = [self.timeLabel.text sizeWithFont:_timeLabel.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) lineBreakMode:NSLineBreakByCharWrapping].width;
    [_actNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(LYScreenWidth() - 38 - timeWidth);
    }];
    [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_greaterThanOrEqualTo(timeWidth);
    }];
}

@end
